package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Section;

@Repository(value = "sectionDao")
@Transactional(readOnly = false)
public class SectionDaoImpl implements SectionDao {
	
	private EntityManager em = null;	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	@SuppressWarnings("unchecked")
	public List<Section> getSectionList() {
		return em.createNamedQuery("Section.findAll").getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Section> getSectionList(Integer idSection){
		Query  q = em.createNamedQuery("Section.findBySectionId");
    	q.setParameter("sectionId",idSection);
    	return q.getResultList();	
	}
	
	@SuppressWarnings("unchecked")
	public List<Section> getSectionListWithIdArticle(Integer idArticle){
		Query  q = em.createNamedQuery("Section.findByArticleId");
    	//q.setParameter("active",active);
    	q.setParameter("articuleId",idArticle);
    	return q.getResultList();	
	}

	@SuppressWarnings("unchecked")
	public List<Section> getSectionWithRegionId(Integer idRegion, boolean active){
		Query  q = em.createNamedQuery("Section.findSectionWithRegionId");
    	q.setParameter("regionId",idRegion);
    	q.setParameter("active",active);
    	return q.getResultList();
	}
	public Section findSectionByID(Integer idSection) {
		Section res=null;
		try {
	    	res=em.find(Section.class,idSection);
		} catch (Exception e) {}
    	return res;
	}
	public void add(Section section) {
		em.persist(section);
	}
	public void update(Section section) {
		em.merge(section);
	}

	public void remove(Section section) {
		//Query q = em.createNamedQuery("Region.deleteByCountryIdByRegionId");
    	//q.executeUpdate();
		em.remove(em.getReference(Section.class,section.getSectionId()));
    	
	}
	public List<Section> getSectionListArticleWithIdArticleActive(Integer idArticle, boolean active) {
		
		Query  q = em.createNamedQuery("Section.findByArticleIdArtive");
    	q.setParameter("articleId",idArticle);
    	q.setParameter("active",active);
    	return q.getResultList();

		//return null;
	}
	public List<Section> getListDistictSectionWithRegionId(Integer idRegion, boolean active){
		Query  q = em.createNamedQuery("Section.findDistinctSectionWithRegionId");
    	q.setParameter("regionId",idRegion);
    	q.setParameter("active",active);
    	return q.getResultList();
	}


}
